package com.example.statelayouthelper

import android.graphics.Color
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cbfg.state.StateLayoutHelper
import com.example.statelayouthelper.databinding.ActivityDemo1Binding

/**
 * @Description: 状态控制 Demo
 * @Author: TomHawk
 * @CreateDate: 2022/8/18 15:27
 */
class Demo1Activity : AppCompatActivity() {
    private lateinit var binding: ActivityDemo1Binding
    private lateinit var statusLayoutHelper: StateLayoutHelper

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityDemo1Binding.inflate(layoutInflater)
        setContentView(binding.root)
        initToolBar()
        initStatusLayout()
        initViewContent()
        initRecycleView()
    }

    /**
     * 初始化toolbar
     */
    private fun initToolBar() {
        binding.tbBar.setTitleTextColor(Color.WHITE)
        binding.tbBar.title = "状态控制 Demo"
        binding.tbBar.inflateMenu(R.menu.menu_demo1)
        binding.tbBar.setOnMenuItemClickListener { item: MenuItem ->
            when (item.itemId) {
                R.id.action_contents -> statusLayoutHelper.showState(STATE_CONTENT)
                R.id.action_emptyData -> statusLayoutHelper.showState(STATE_EMPTY)
                R.id.action_error -> statusLayoutHelper.showState(STATE_ERROR)
                R.id.action_networkError -> statusLayoutHelper.showState(STATE_NETWORK_ERROR)
                R.id.action_loading -> statusLayoutHelper.showState(STATE_LOADING)
                R.id.actionHideEmpty -> statusLayoutHelper.hideState(STATE_EMPTY)
                R.id.actionHideError -> statusLayoutHelper.hideState(STATE_ERROR)
                R.id.actionHideNetworkError -> statusLayoutHelper.hideState(STATE_NETWORK_ERROR)
                R.id.actionHideLoading -> statusLayoutHelper.hideState(STATE_LOADING)
                R.id.actionHideAll -> statusLayoutHelper.hideStates()
            }
            true
        }
    }

    private fun initStatusLayout() {
        statusLayoutHelper = StateLayoutHelper.Builder(binding.vgMain)
            .addState(STATE_CONTENT, STATE_CONTENT, false)
            .addState(STATE_EMPTY, STATE_EMPTY)
            .addState(STATE_ERROR, STATE_ERROR)
            .addState(STATE_LOADING, STATE_LOADING)
            .addState(STATE_NETWORK_ERROR, STATE_NETWORK_ERROR)
            .setStateClickListener { helper, state, view ->
                if (state == STATE_NETWORK_ERROR) {
                    //网络异常，点击重试:
                    helper.showState(STATE_LOADING)
                } else if (state == STATE_LOADING) {
                    //取消加载:
                    if (view.id == R.id.btnStateCancel) {
                        Toast.makeText(this@Demo1Activity, "兄台，加载取消啦！", Toast.LENGTH_SHORT).show()
                        helper.showState(STATE_CONTENT)
                    }
                } else {
                    helper.showState(STATE_CONTENT)
                }
            }
            .build()
    }

    private fun initViewContent() {
        findViewById<View>(R.id.btn_empty).setOnClickListener {
            statusLayoutHelper.showState(
                STATE_EMPTY,
                R.drawable.ic_state_empty,
                "兄台，没有数据"
            )
        }
        findViewById<View>(R.id.btn_error).setOnClickListener {
            statusLayoutHelper.showState(
                STATE_ERROR,
                R.drawable.ic_state_network_error,
                "兄台，错误数据"
            )
        }
        findViewById<View>(R.id.btn_network_error).setOnClickListener {
            statusLayoutHelper.showState(STATE_NETWORK_ERROR)
        }
        findViewById<View>(R.id.btn_test).setOnClickListener {
            statusLayoutHelper.removeState(STATE_EMPTY)
        }
        findViewById<View>(R.id.btn_test2).setOnClickListener {
            statusLayoutHelper.removeState(STATE_ERROR)
        }
        findViewById<View>(R.id.btn_test3).setOnClickListener {
            statusLayoutHelper.clearStates()
        }
    }

    private fun initRecycleView() {
        val recyclerView = findViewById<View>(R.id.recycleView) as RecyclerView
        recyclerView.layoutManager = LinearLayoutManager(this)
        val adapter = Demo1Adapter(
            Array(5) { i -> "这是第${i}条数据" }.toList(),
            this
        )
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener { _, position ->
            Toast.makeText(this, "条目${position}被点击呢", Toast.LENGTH_SHORT).show()
        }
        statusLayoutHelper.showState(STATE_CONTENT)
    }

    companion object {
        private const val STATE_CONTENT = R.layout.activity_demo1_content
        private const val STATE_EMPTY = R.layout.layout_state_empty
        private const val STATE_ERROR = R.layout.layout_state_error
        private const val STATE_LOADING = R.layout.layout_state_loading
        private const val STATE_NETWORK_ERROR = R.layout.layout_state_network_error
    }
}